/**
 * 
 */
package edu.umd.clip.lm.model.data;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.*;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class OnDiskTrainingDataReader extends TrainingDataReader {
	private ReadableByteChannel input;
	
	public OnDiskTrainingDataReader(ReadableByteChannel input) {
		this.input = input;
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.model.training.TrainingDataReader#getNextBlock()
	 */
	@Override
	public TrainingDataBlock getNextBlock() {
		try {
			TrainingDataBlock block = new TrainingDataBlock(input);
			/*
			if (block.isTerminator()) {
				finishedReadingData();
			}
			*/
			return block;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}		
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.model.training.TrainingDataReader#start(edu.umd.clip.lm.model.training.ReadableTrainingData)
	 */
	@Override
	public void start(ReadableTrainingData data) {
		try {
			ByteBuffer buffer = ByteBuffer.allocate(AbstractTrainingData.HEADER_SIZE);
			input.read(buffer);
			buffer.flip();
			data.readHeader(buffer);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
